package cn.com.czpb.client.module.live.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.live.LiveItemBean;
import cn.com.czpb.client.model.live.LiveListEntity;
import cn.com.czpb.client.model.livenew.LiveNewListBean;
import cn.com.czpb.client.module.live.adapter.LiveRecycleAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/12/1.
 */

public class LiveListActivity extends MToolBarActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {
    @BindView(R.id.recycler_id)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;


    @BindView(R.id.ll_back)
    LinearLayout ll_back;

    private String type;
    private int page = 1;
    private static final int pageSize = 15;//
    private boolean mFlagRefresh = true;
    private LiveRecycleAdapter adapter;


    @Override
    public void initView() {
        type = getIntent().getStringExtra("type");
        initAdapter();
        getActiveListData();

    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_livelist;
    }

    @Override
    public void initData() {
        super.initData();
    }

    /**
     * adapter init
     */
    private void initAdapter() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        refresh.setRefreshHeader(new ClassHeader_other(activity));
        refresh.setOnRefreshListener(this);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        adapter = new LiveRecycleAdapter(null, spu.getShowImage());
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        mRecyclerView.setAdapter(adapter);
    }
    /**
     * 获取视频列表数据
     */
    private void getActiveListData() {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("Page", page + "");
        map.put("type", type);
        map.put("PageSize", pageSize + "");
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        LogUtils.i("device-->" + DeviceUtils.getMyUUID(activity));
        Factory.provideHttpService().liveList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<LiveListEntity>() {
                    @Override
                    public void call(LiveListEntity entity) {
                        setListData(entity.data);
                        refresh.finishRefresh();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("异常-->" + throwable.toString());
                        adapter.loadMoreFail();
                    }
                });
    }
    public void setListData(List<LiveItemBean> list) {

        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < pageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
        refresh.setEnabled(true);


    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        LiveItemBean itemBean = (LiveItemBean) adapter.getItem(position);
        switch (view.getId()) {
            case R.id.rl_id:

                PageCtrl.start2LiveDetailActivity(activity,itemBean);
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        refresh.setEnabled(false);
        mFlagRefresh = false;
        page++;
        getActiveListData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1;
                mFlagRefresh = true;
                getActiveListData();
            }
        }, 2000);
    }
}
